package com.far.dao;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import oracle.jdbc.OracleTypes;
import utiles.AppProperties;
import utiles.ConexionPool;

import com.far.dto.DocumentoDTO;

public class FarDocumentoPacDAO {
	private static Logger log = Logger.getLogger(FarDocumentoPacDAO.class);
	String pkgVar = (AppProperties.getProperty("aplicacion", "PACKAGE"));
	
	public  List<DocumentoDTO> getBoletasPaciente(String rutp) throws SQLException, IOException{

		Connection conn = ConexionPool.getConnectionPool();	

		ArrayList<DocumentoDTO> listRetorno = new ArrayList<DocumentoDTO>();
		try {
			String query = "{ call "+pkgVar+".PKG_PAC_PACIENTE.getBoletasPac(?,?) }"; 	       
			CallableStatement  stmt = conn.prepareCall(query);
			stmt.setString(1, rutp);
			stmt.registerOutParameter(2, OracleTypes.CURSOR);
			stmt.execute();
			ResultSet rs = (ResultSet)stmt.getObject(2);
			while (rs.next()) {
				DocumentoDTO documentopaciente = new DocumentoDTO();
				documentopaciente.setIdDocumento(rs.getString(1));
				documentopaciente.setFecEmision(rs.getString(2)); 
				documentopaciente.setTotal(rs.getString(3));
				
				listRetorno.add(documentopaciente);				
			}
			rs.close();
			stmt.close();
		} catch (SQLException ex) {  
			log.error("Error FarDocumentoPacDAO.getBoletasPaciente SQLException: " +ex.getMessage());
			System.out.println(ex.getMessage());
		} finally {			
			conn.close();
			conn = null;
		}
		return listRetorno;
	}
	
	
	public  int getDocumentosByfolio(String folio) throws SQLException, IOException{

		Connection conn = ConexionPool.getConnectionPool();	
		int valor=9;

		
		try {
			String query = "{ call "+pkgVar+".PKG_PAC_PACIENTE.getDocumentosByRut(?,?) }"; 	       
			CallableStatement  stmt = conn.prepareCall(query);
			stmt.setString(1, folio);
			stmt.registerOutParameter(2, OracleTypes.INTEGER);
			stmt.execute();
			valor=stmt.getInt(2);
			//System.out.println(valor);
			stmt.close();
		} catch (SQLException ex) {  
			log.error("Error FarDocumentoPacDAO.getBoletasPaciente SQLException: " +ex.getMessage());
			System.out.println(ex.getMessage());
		} finally {			
			conn.close();
			conn = null;
		}
		return valor;
	}
	
	
}
